package com.example.yichuangyun.mvvm.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.Observer;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentBusinessRecordBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.order.OrderDetailAllFollowModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.ui.activity.order.view.OrderDetailsViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.ParentOrderFollowAdapter;
import com.example.yichuangyun.utils.RequestUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单详情-跟进
 */
public class OrderFollowFragment extends BaseFragment<FragmentBusinessRecordBinding, OrderDetailsViewModel> {

    private String id;
    private List<OrderDetailAllFollowModel> data;
    private ParentOrderFollowAdapter adapter;
    private MyDialog myDialog;
    private String mTelNumber, clientName;

    @Override
    protected OrderDetailsViewModel bindModel() {
        return getViewModel(this, OrderDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {
        initDialog();
    }

    @Override
    protected void lazyLoadData() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getString(Constant.idKey);
            mTelNumber = bundle.getString(Constant.phoneKey);
        }
        data = new ArrayList<>();
        data.add(new OrderDetailAllFollowModel(getString(R.string.order_detail_order_follow)));
        data.add(new OrderDetailAllFollowModel(getString(R.string.order_detail_service_follow)));
        data.add(new OrderDetailAllFollowModel(getString(R.string.order_detail_custom_follow)));
        adapter = new ParentOrderFollowAdapter(mContext);
        binding.recyclerView.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(mContext, R.layout.layout_empty_list2);
        viewModel.getOrderDetailsForOrderFollow(id);
        initMyClick();
    }

    protected void initMyClick() {
        viewModel.followMutableLiveData.observe(this, orderFollowDataModel -> {
            if (orderFollowDataModel != null) {
                if (orderFollowDataModel.getOrderList() != null) {
                    data.get(0).setOrderFollowList(orderFollowDataModel.getOrderList());
                }
                if (orderFollowDataModel.getTrackList() != null) {
                    data.get(1).setTrackList(orderFollowDataModel.getTrackList());
                }
                viewModel.getOrderDetailsForVisit(id);
            }
        });

        viewModel.mErrorData.observe(this, s -> viewModel.getOrderDetailsForVisit(id));

        viewModel.visitListMutableLiveData.observe(this, visitModels -> {
            if (visitModels != null) {
                data.get(2).setVisitList(visitModels);
            }
            adapter.submitList(data);
        });

        viewModel.visitListErrorMutableLiveData.observe(this, s -> adapter.submitList(data));

        adapter.setOnCallListener(userName -> {
            this.clientName = userName;
            RequestUtils.checkCallNum(mContext, mTelNumber, myDialog, 0, false);
        });
    }

    public static OrderFollowFragment newInstance(String id, String mTelNumber) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.idKey, id);
        bundle.putString(Constant.phoneKey, mTelNumber);
        OrderFollowFragment fragment = new OrderFollowFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initDialog() {
        myDialog = new MyDialog((Activity) mContext) {
            @Override
            public void leftClick() {
                RequestUtils.callNum(mContext, mTelNumber);
                Constant.pushBusinessId = "";
            }

            @Override
            public void rightClick() {
                RequestUtils.callbackOutbound(mContext, mTelNumber, null, clientName);
            }
        };
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusModel eventBusModel) {
        if (null != eventBusModel) {
            if (eventBusModel.getMsg().equals(Constant.refreshOrderFollowList)) {
                adapter = new ParentOrderFollowAdapter(mContext);
                binding.recyclerView.setAdapter(adapter);
                viewModel.getOrderDetailsForOrderFollow(id);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
